package com.lzy.mall.member.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.lzy.mall.common.constant.Constant;
import com.lzy.mall.common.exception.CommonError;
import com.lzy.mall.common.utils.CommonUtil;
import com.lzy.mall.common.utils.PageParaUtils;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.member.entity.UmsMember;
import com.lzy.mall.member.dao.UmsMemberDao;
import com.lzy.mall.member.entity.UmsMemberLevel;
import com.lzy.mall.member.service.UmsMemberLevelService;
import com.lzy.mall.member.service.UmsMemberService;
import com.lzy.mall.member.vo.login.UserLoginVo;
import com.lzy.mall.member.vo.login.UserRegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


/**
 * 会员(UmsMember)表服务实现类
 *
 * @author lzy
 * @since 2021-08-25 11:38:45
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class UmsMemberServiceImpl implements UmsMemberService {
    private final UmsMemberDao umsMemberDao;

    private final UmsMemberLevelService umsMemberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);
        PageQuery<UmsMember> page = umsMemberDao.queryPage(pageQuery);
        return new PageUtils(page);
    }

    @Override
    public ResultUtils register(UserRegisterVo userRegisterVo) {
        String userName = userRegisterVo.getUserName();
        String phone = userRegisterVo.getPhone();
        String password = userRegisterVo.getPassword();

        if (StrUtil.hasEmpty(userName,password,phone)){
            return ResultUtils.error(CommonError.BIND_ARGS_ERROR);
        }
        if (!CommonUtil.isPhone(phone)){
            return ResultUtils.error(CommonError.PHONE_ERROR);
        }
        long count = umsMemberDao.createLambdaQuery()
                .orEq(UmsMember::getUsername, userName)
                .orEq(UmsMember::getMobile, phone)
                .count();
        if (count > 0){
            return ResultUtils.error(CommonError.ACCOUNT_OR_PHONE_ISSINGIN);
        }

        // 设置默认等级
        UmsMemberLevel defaultInfo = umsMemberLevelService.getDefaultInfo();

        UmsMember build = UmsMember.builder()
                .username(userName)
                .mobile(phone)
                .password(SecureUtil.md5(password + Constant.SALT))
                .levelId(defaultInfo.getId())
                .createTime(DateUtil.date())
                .nickname(userName)
                .status(0)
                .gender(0)
                .integration(0)
                .growth(0)
                .build();
        umsMemberDao.insertTemplate(build);

        log.info("注册成功");
        return ResultUtils.ok();
    }

    @Override
    public ResultUtils login(UserLoginVo vo) {
        String username = vo.getLoginacct();
        String password = vo.getPassword();
        if (StrUtil.isEmpty(username)){
            return ResultUtils.error(CommonError.ACCOUNTNO_ISNULL);
        }
        if (StrUtil.isEmpty(password)){
            return ResultUtils.error(CommonError.USERPASSWORD_ISNULL);
        }
        UmsMember member = umsMemberDao.createLambdaQuery()
                .orEq(UmsMember::getUsername, username)
                .orEq(UmsMember::getMobile, username)
                .single();
        if (ObjectUtil.isNull(member)){
            return ResultUtils.error(CommonError.ACCOUNTNO_ERROR);
        }
        String pwd = member.getPassword();
        String md5 = SecureUtil.md5(password + Constant.SALT);

        if (!pwd.equals(md5)){
            return ResultUtils.error(CommonError.USERPASSWORD_ERROR);
        }

        log.info("登录成功");
        return ResultUtils.ok(member,"登录成功");
    }
}
